/*----------------------------------------------
| noka charts v1.0 www.97521.com               |
| rebin 2014-01-15                             |
|---------------------------------------------*/
var NStatCharts = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.cfg = cfg;					//配置信息
    	this.id=cfg.id; //id
    	this.rurl=cfg.rurl;
    	this.width=cfg.width;//宽
    	this.height=cfg.height;//高
    	this.xmlpath=cfg.xmlpath;
    	this.sfun=cfg.sfun;
    	this.type=cfg.type;
    	this.parsva = cfg.parsva || [];//参数 [{name:'',value:''},{}]
    },
    show : function(){
    	this.InitData();//加载数据
    	this.EventFunction();//加载方法
    },
    //------------初始化数据------------------------------
    InitData : function(){
    	var self = this;
    	self.LoadData(self.parsva);
    },
    //-------------加载数据-----------------------------
    LoadData : function(pars){
    	var self = this;
    	$(self.id).innerHTML='<img alt="loading...." src="'+self.rurl+'/nokatag/tablegrid/images/mt_load.gif"></img>';//显示加载动画
    	if(self.xmlpath!=undefined){
    		self.XMLajaxLoadData(pars);
    	}else if(self.sfun!=undefined){
    		self.SfunAjaxLoadData(pars);
    	}else{
    		self.initFlash();
    	}
    },
  //-------------初始化本地数据---------------------------
    initFlash : function(){
    	var self = this;
    	var flashUrl = self.rurl+"/nokatag/statchart/file/"+self.type;
    	var nchartsf = new FusionCharts(flashUrl, self.id+'_flashID', self.width, self.height,"0","0");
    	nchartsf.setDataXML($(self.id+'_data').innerHTML);//加载数据
    	nchartsf.render(self.id);//初始化显示
    },
    //--------ajax 请求数据------------------------------------
    XMLajaxLoadData : function(pars){
    	var self = this;
    	new Ajax.Request(self.xmlpath, {
    		method:'post',
            parameters: self.parsvInit(pars),
            onSuccess: function(response) {
            	$(self.id+'_dav').innerHTML='a<div id="'+self.id+'_data">'+response.responseText+'</div>';
            	self.initFlash();//显示
            }
    	});
    },
    //---------服务器方法加载数据---------------------------------
    SfunAjaxLoadData : function(pars){
    	var self = this;
    	new Ajax.Request(self.rurl+'/nokatag/ajax/inputtextcheck'+self.id+'.statchart?fun='+self.sfun, {
    		method:'post',
            parameters: self.parsvInit(pars),
            onSuccess: function(response) {
            	$(self.id+'_dav').innerHTML='a<div id="'+self.id+'_data">'+response.responseText+'</div>';
            	self.initFlash();//显示
            }
    	});
    },
    //------------组枳参数--------------------
    parsvInit : function(pars){
    	var idx = 0;//
    	var parsv=[];
    	for(var i=0;i<pars.length;i++){
    		parsv[idx++]='&'+pars[i].name+'='+pars[i].value;
    	}
    	return parsv.join('');
    },
    //-----------加载方法-----------------------
    EventFunction : function(){
    	var self = this;
    	$(self.id).LoadData = function(a){
    		if(a==undefined)
    			self.LoadData([]);
    		else
    			self.LoadData(a);
    	};
    }
});